package main.java.pers.seg.model.utils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.*;

import static main.java.pers.seg.model.constants.ObjectConstants.viewcontainermap;

public class MenuUtils {

    public static void start1(String filePath,String newFilePath) throws Exception {
        if (filePath==null || filePath.isEmpty()){
            throw new Exception("filePath could not be null or empty!");
        }
        File file = new File(filePath);
        if(!file.exists() || !file.isFile() || !(file.getName().endsWith(".xml") || file.getName().endsWith(".core")))
            throw new Exception(filePath+" is not a xml file");
        if (!file.canRead()){
            throw new Exception("can not read "+filePath);
        }
        SAXReader reader = new SAXReader();
        Document document = reader.read(file);
        Element root = document.getRootElement();
        forXml(document,root);

        String filename = file.getName().substring(0,file.getName().indexOf("."));
        File newFile = new File(newFilePath+filename+file.getName());
        FileOutputStream out = new FileOutputStream(newFile);
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(out,format);
        xmlWriter.write(document);
        out.close();

    }

    public static void forXml(Document document,Element e){
        String name = e.getName();
        if (name.equals("View")){
            String type = e.attributeValue("type");
            if(type!=null && !type.isEmpty() && type.equals("android,view.Menu")){
                Element parent = e.getParent();
                if (parent!=null && parent.getName().equals("View")){
                    String ptype = parent.attributeValue("type");
                    if(ptype!=null && !ptype.isEmpty() && !ptype.equals("android.support.design.widget.NavigationView")){
                        dealXml(document, e);
                    }else {
                        elseforXml(document,e);
                    }
                }else {
                    elseforXml(document,e);
                }
            }else {
                elseforXml(document,e);
            }
        }else {
            elseforXml(document,e);
        }

    }

    public static void dealXml(Document document,Element e){
        Element copy = (Element) e.clone();
        copy.addAttribute("source",findFisrtNode(e));
        document.getRootElement().add(copy);
        document.remove(e);
    }

    /**
     * 返回父节点名 用于添加在外层MenuList 作为属性
     * @param e
     * @return
     */
    public static String findFisrtNode(Element e){
        Element p = e.getParent();
        if(p!=null){
            String name = p.getName();
            if (viewcontainermap.containsKey(name))
                return name;
            String namea = p.attributeValue("name");
            if (viewcontainermap.containsKey(namea))
                return namea;
            return findFisrtNode(p);
        }
        return null;
    }

    public static void elseforXml(Document document,Element e){
        List<Element> list = e.elements();
        for (Element es:list)
            forXml(document,es);
    }
}
